<template>
  <view class="login-container">
    <!-- 登录的图标 -->
    <uni-icons type="contact-filled" size="100" color="#afafaf"></uni-icons>
    <!-- 登录的按钮 -->
    <!-- 新版 -->
    <button type="primary" class="btn-login" @click="getUserProfile" v-if="canIUseGetUserProfile">一键登录</button>
    <!-- 旧版 -->
    <button type="primary" class="btn-login" open-type="getUserInfo" @getuserinfo="getUserInfo" v-else>一键登录</button>
    <!-- 登录提示 -->
    <view class="tips-text">
      登录后拥有更多权限
    </view>
  </view>
</template>

<script>
  import {
    mapMutations,
    mapState
  } from 'vuex'
  export default {
    name: "my-login",
    props: {
      // 背景颜色
      canIUseGetUserProfile: {
        type: Boolean,
        default: false
      },
    },
    data() {
      return {

      };
    },
    methods: {
      ...mapMutations('m_user', ['updateUserInfo', 'updateToken', 'updateRedirectInfo']),
      // 旧版 获取微信用户的基本信息
      getUserInfo(e) {
        // 判断是否获取用户信息成功
        if (e.detail.errMsg === 'getUserInfo:fail auth deny') return uni.$showMsg('您取消了登录授权！')
        // 获取用户信息成功， e.detail.userInfo 就是用户的基本信息
        console.log(e.detail.userInfo)
        this.updateUserInfo(e.detail.userInfo)
        // 假数据
        // let token ="Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjEyLCJpYXQiOjE1MjU0MDIyMjMsImV4cCI6MTUyNTQ4ODYyM30.g-4GtEQNPwT_Xs0Pq7Lrco_9DfHQQsBiOKZerkO-O-o"
        let token = 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo'
        this.updateToken(token)
        this.navigateBack()
      },
      // 新版 获取微信用户的基本信息
      getUserProfile() {
        // 2.10.0以上版本登录信息获取方式
        wx.getUserProfile({
          desc: "用于获取信息",
          success: (res) => {
            this.updateUserInfo(res.userInfo)
            // 假数据
          let token = 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo'
            this.updateToken(token)
            this.navigateBack()

          },
          fail: () => {
            uni.$showMsg('您取消了登录授权！')
          }
        })
      },
      navigateBack() {
        if (this.redirectInfo && this.redirectInfo.openType == 'switchTab') {
          uni.switchTab({
            url: this.redirectInfo.from,
            success: () => {
              this.updateRedirectInfo({})
            }
          })
        }
        if (this.redirectInfo && this.redirectInfo.openType == 'switchTab') {
          uni.switchTab({
            url: this.redirectInfo.from
          })
        }
      }
    },
    computed: {
      ...mapState('m_user', ['redirectInfo']),
    }

  }
</script>

<style lang="scss">
  .login-container {
    height: 750rpx;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background-color: #F8F8F8;
    position: relative;
    overflow: hidden;

    // 绘制登录盒子底部的半椭圆造型
    &:after {
      content: '';
      display: block;
      position: absolute;
      width: 100%;
      height: 60px;
      left: 0;
      bottom: 0;
      background-color: white;
      border-radius: 100%;
      transform: translateY(50%);
    }

    // 登录按钮的样式
    .btn-login {
      width: 90%;
      border-radius: 100px;
      margin: 15px 0;
      background-color: #c00000;
    }

    // 按钮下方提示消息的样式
    .tips-text {
      font-size: 12px;
      color: gray;
    }
  }
</style>
